package com.lin.csdn.service.impl;

import com.lin.csdn.dao.impl.BlogDao;
import com.lin.csdn.dao.impl.CommentDao;
import com.lin.csdn.utils.DateView;
import com.lin.csdn.utils.ResultSplit;

import java.util.List;

public class CommentService {

    CommentDao commentDao=new CommentDao();
    ResultSplit split=new ResultSplit();
    DateView dateView=new DateView();
    BlogDao blogDao=new BlogDao();

    //通过blogid查询相关此博客的评论
    public List<Object> blogIdSearch(String blogId,int beginIndex,int pageSize){
        Object result=commentDao.blogIdSearch(Integer.parseInt(blogId.replace(" ","")),beginIndex,pageSize);
        return split.resultSplit(result);
    }

    //通过用户名获取属于自己的评论
    public List<Object> userNameSearch(String userName,int beginIndex,int pageSize){
        Object result=commentDao.userNameSearch(userName,beginIndex,pageSize);
        return split.resultSplit(result);
    }

    //删除指定评论，通过id
    public void commentIdDelete(String commentId){
        commentDao.commentIdDelete(Integer.parseInt(commentId.replace(" ","")));
    }

    //添加新的评论
    public int addComment(String context,String blogId,String username){
        String date=dateView.getDate();
        int newCommentID=commentDao.addComment(context,Integer.parseInt(blogId.replace(" ","")),date,username);
        int n=context.indexOf("@");
        if(n!=-1){
            String[] body=context.split(" ");
            String[] userName=body[0].split("@");
            String title=blogDao.searchTitle(Integer.parseInt(blogId.replace(" ","")));
            commentDao.addNotify(username,title,userName[1],date);
        }
        return newCommentID;
    }

    //查找有关自己的通知
    public List<Object> searchNotify(String userName){
        Object context=commentDao.searchNotify(userName);
        return split.resultSplit(context);
    }
}
